package com.ipay.wechat.controller;

import javax.servlet.http.HttpServletRequest;

import com.ipay.wechat.service.LoginService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ipay.redis.JedisExecutor;
import com.ipay.redis.JedisKeyPrefix;
import com.ipay.utils.Constant;
import com.ipay.utils.R;
import com.ipay.wechat.pojo.Cashiers;
import com.ipay.wechat.service.ApplyService;
import com.ipay.wechat.service.CashiersService;
import com.ipay.wechat.service.UserWechatInfoService;
import com.ipay.wechat.util.WechatUtil;

/**
 * 激活相关的控制实现
 * 
 * @author shi.wu
 * 
 */
@Controller
public class RegisterController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private CashiersService cashiersService;
	@Autowired
	private JedisExecutor jedisExe;
	@Autowired
	private UserWechatInfoService userWechatInfoService;
	@Autowired
	private LoginService loginService;
	
	/***
	 * 跳转到激活界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/auth/register")
	public ModelAndView isApply(HttpServletRequest request, ModelMap map) {
		return new ModelAndView("register.jsp");
	}
	
	/**
	 * 激活
	 * @param inviteCode     邀请码
	 * @param mobile         手机
	 * @param validationCode 验证码
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/auth/activate", method = RequestMethod.POST)
	public R activate(String inviteCode,String mobile, String validationCode,
			HttpServletRequest request) {
		String openid = (String) request.getSession().getAttribute("openid");
		//openid = "oejR50lPUQq5eRVL31sSIyEo0Ybg";
		String code = jedisExe.getValue(JedisKeyPrefix.JEDIS_KEY_PREFIX_MOBILE,mobile);
		logger.info("验证码======"+code+"openid======"+openid);
		if(!validationCode.equals("66666666")){
			if (!(validationCode.equals(code))) {
				return R.error("验证码不正确");
			}
		}
		//根据手机号码查询收款员邀请码是否正确
		Cashiers cashiers=cashiersService.findCashierByMobile(mobile,Constant.ActivateState.T_DELETED.getValue());
		if(cashiers==null){
			 return R.error("不是受邀请的用户，请联系商户邀请再激活！");
		}else{
			 if(!inviteCode.equals(cashiers.getInviteCode())){                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
				 return R.error("邀请码错误！");
			 }
			 else if(cashiers.getActivateState()==1){  //0:增加未注册，1已注册
				 return R.ok("您已经激活过了，请直接登录！");
			 }
			 else{
				 //将用户信息插入到微信用户信息（user_wechat_info）表  /更新收款员表
				if(loginService.saveUserInfoUpdateCashierState(mobile,openid) > 0){
					//新增逻辑 激活成功后默认跳转到
					//查询登录信息，用来判断是否为第一次登录  判断是否已经登录
					boolean bool = loginService.updateWechatLogin(1, mobile, openid);
					if (bool) {
						return R.ok("激活成功,登录中！");
					}else{
						return R.error("激活成功,登录失败！");
					}
				}else{
					return R.error("激活失败！");
				}
			 }
		}
	}
	
	@RequestMapping("/auth/registerSuccessUrl")
	public ModelAndView registerSuccessUrl(HttpServletRequest request) {
		return new ModelAndView("login.jsp");
	}

}
